﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using FineUI;
using SysManage.BLL;
using ZHDJ.Core.Util;
using ZHDJ.Data.Models.Center;
using ZHDJ.Data.Models.Common;
using ZHDJ.WebPage;
using System.Text;
using System.Data;
using ZHDJ.Data.Models.Org;

namespace SysManage.Module.ScoreManage
{
    public partial class PartyObjectScoreEdit : PageBase
    {
        #region 属性

        #endregion
        #region ViewPower

        /// <summary>
        /// 本页面的浏览权限，空字符串表示本页面不受权限控制
        /// </summary>
        /*public override string ViewPower
        {
            get { return "PartyObjectScoreAdd"; }
        }*/

        #endregion

        public string ObjectType
        {
            get {
                return this.Request["ObjectType"];
            }
        }

        #region 页面属性

        public string EditorContent { get; set; }

        #endregion

        #region Page_Load

        protected void Page_Load(object sender, EventArgs e)
        {

            string UserName = this.CurrentUser.LoginID;

            if (UserName == "admin")
            {

                ddlStatus.Hidden = false;
                ddlStatus.Required = true;
                txtAduitContent.Hidden = false;
                txtAduitContent.Required = true;
            }
            else {
                if (ObjectType == "member")
                {
                    string ssss = this.CurrentUser.IsPartyWorker.ToString();
                    if (ssss == "True")
                    {
                        ddlStatus.Hidden = false;
                        ddlStatus.Required = true;
                        txtAduitContent.Hidden = false;
                        txtAduitContent.Required = true;

                    }
                    else {
                        ddlStatus.Hidden = true;
                        ddlStatus.Required = false;
                        txtAduitContent.Hidden = true;
                        txtAduitContent.Required = false;
                    }
                }
                else {
                    ddlStatus.Hidden = true;
                    ddlStatus.Required = false;
                    txtAduitContent.Hidden = true;
                    txtAduitContent.Required = false;
                }
                
            }
            
            if (!IsPostBack)
            {
                BindData();
                LoadData();
            }
        }

        private void BindData()
        {
            btnClose.OnClientClick = ActiveWindow.GetHideReference();
        }

        private void LoadData()
        {
            string id = GetQueryIntValue("id");
            if (id != "")
            {
                var current = DB.PartyObjectScoreApplys.Find(id);
                if (current == null)
                {
                    // 参数错误，首先弹出Alert对话框然后关闭弹出窗口
                    Alert.Show("参数错误！", String.Empty, ActiveWindow.GetHideReference());
                    return;
                }
                txtTitle.Text = current.Title;
                txtContent.Text = current.Content;
                txtScore.Text = current.Score.ToString();
                filePhoto.Text = current.ImageID;
                ddlStatus.SelectedValue = current.Status.ToString();
                txtAduitContent.Text = current.AduitContent;


                if (current.Status == 4)
                {
                    ddlStatus.Enabled = false;
                }
                hdImageId.Text = current.ImageID;
                if (!string.IsNullOrEmpty(hdImageId.Text))
                {
                    filePhoto.EmptyText = UpFileBusiness.GetFileName(hdImageId.Text);
                    HyperLink1.ImageID = hdImageId.Text;
                }
                if (ObjectType == "branch")
                {
                    if (this.CurrentUser.LoginID == "admin")
                    {
                        ppBranch.Value = current.ObjectID;
                        ppBranch.Text = ControlBusiness.GetBranchPickerText(current.ObjectID);
                        ppBranch.Hidden = false;
                    }
                    else
                    {
                        ppBranch.Value = this.CurrentUser.BranchID;//获取当前所属组织ID
                        ppBranch.Text = ControlBusiness.GetBranchPickerText(this.CurrentUser.BranchID);//获取当前所属组织名称
                        ppBranch.Hidden = true;
                    }
                    //ppMember.Hidden = true;
                }
                else
                {
                    ppBranch.Hidden = true;
                }
                /*
                if (ObjectType=="member") {
                    ppBranch.Hidden = true;
                    IQueryable<PartyObjectScoreApply> q = DB.PartyObjectScoreApplys.Where(t => t.ID == id);

                    if (q.Count() > 0)
                    {
                        StringBuilder sbLoginID = new StringBuilder();
                        StringBuilder sbName = new StringBuilder();

                        foreach (PartyObjectScoreApply item in q)
                        {
                            sbLoginID.Append(item.ObjectID + ",");
                            sbName.Append(ControlBusiness.GetMembersPickerText(item.ObjectID) + ",");
                        }

                        ppMember.Value = sbLoginID.ToString().TrimEnd(',');
                        ppMember.Text = sbName.ToString().TrimEnd(',');
                    }
                }*/

                if (current.ObjectType == "Member")
                {
                    ppBranch.Hidden = true;
                    var current1 = DB.PartyOrgBranchs.Find(this.CurrentUser.BranchID);
                    string str = this.CurrentUser.IsPartyWorker.ToString();
                    string bm = current1.BranchMark;
                    if (str == "True")
                    {
                        ddlStatus.Hidden = false;
                        ddlStatus.Required = true;
                        txtAduitContent.Hidden = false;
                        txtAduitContent.Required = true;
                        if (bm == "2")
                        {
                            ddlStatus.Items.Insert(0, new FineUI.ListItem() { Text = "==请选择==", Value = "" });
                            ddlStatus.Items.Insert(1, new FineUI.ListItem() { Text = "通过", Value = "7" });
                            ddlStatus.Items.Insert(2, new FineUI.ListItem() { Text = "驳回", Value = "9" });
                            if (current.Status == 7)
                            {
                                txtTitle.Readonly = true;
                                txtContent.Readonly = true;
                                txtScore.Readonly = true;
                                filePhoto.Readonly = true;
                                txtAduitContent.Readonly = true;
                                ddlStatus.Readonly = true;
                            }
                        }
                        if (bm == "3")
                        {
                            ddlStatus.Items.Insert(0, new FineUI.ListItem() { Text = "==请选择==", Value = "" });
                            ddlStatus.Items.Insert(1, new FineUI.ListItem() { Text = "通过", Value = "3" });
                            ddlStatus.Items.Insert(2, new FineUI.ListItem() { Text = "驳回", Value = "5" });
                            if (current.Status == 3)
                            {
                                txtTitle.Readonly = true;
                                txtContent.Readonly = true;
                                txtScore.Readonly = true;
                                filePhoto.Readonly = true;
                                txtAduitContent.Readonly = true;
                                ddlStatus.Readonly = true;
                            }
                        }
                        if (bm == "")
                        {
                            ddlStatus.Items.Insert(0, new FineUI.ListItem() { Text = "==请选择==", Value = "" });
                            ddlStatus.Items.Insert(1, new FineUI.ListItem() { Text = "通过", Value = "3" });
                            ddlStatus.Items.Insert(2, new FineUI.ListItem() { Text = "驳回", Value = "5" });
                            if (current.Status == 3)
                            {
                                txtTitle.Readonly = true;
                                txtContent.Readonly = true;
                                txtScore.Readonly = true;
                                filePhoto.Readonly = true;
                                txtAduitContent.Readonly = true;
                                ddlStatus.Readonly = true;
                            }
                        }
                        if (bm == "1")
                        {
                            ddlStatus.Items.Insert(0, new FineUI.ListItem() { Text = "==请选择==", Value = "" });
                            ddlStatus.Items.Insert(1, new FineUI.ListItem() { Text = "通过", Value = "4" });
                            ddlStatus.Items.Insert(2, new FineUI.ListItem() { Text = "驳回", Value = "8" });

                        }
                        if (current.Status == 4)
                        {
                            txtTitle.Readonly = true;
                            txtContent.Readonly = true;
                            txtScore.Readonly = true;
                            filePhoto.Readonly = true;
                            txtAduitContent.Readonly = true;
                            ddlStatus.Readonly = true;
                        }
                    }
                    else
                    {
                        ddlStatus.Hidden = true;
                        ddlStatus.Required = false;
                        txtAduitContent.Hidden = true;
                        txtAduitContent.Required = false;
                    }
                }

            }
            else {
                if (ObjectType == "member")
                {
                    ppBranch.Hidden = true;
                    var current1 = DB.PartyOrgBranchs.Find(this.CurrentUser.BranchID);
                    string str = this.CurrentUser.IsPartyWorker.ToString();
                    string bm = current1.BranchMark;
                    if (str == "True")
                    {
                        ddlStatus.Hidden = false;
                        ddlStatus.Required = true;
                        txtAduitContent.Hidden = false;
                        txtAduitContent.Required = true;
                        if (bm == "2")
                        {
                            ddlStatus.Items.Insert(0, new FineUI.ListItem() { Text = "==请选择==", Value = "" });
                            ddlStatus.Items.Insert(1, new FineUI.ListItem() { Text = "通过", Value = "7" });
                            ddlStatus.Items.Insert(2, new FineUI.ListItem() { Text = "驳回", Value = "9" });
                        }
                        if (bm == "3")
                        {
                            ddlStatus.Items.Insert(0, new FineUI.ListItem() { Text = "==请选择==", Value = "" });
                            ddlStatus.Items.Insert(1, new FineUI.ListItem() { Text = "通过", Value = "3" });
                            ddlStatus.Items.Insert(2, new FineUI.ListItem() { Text = "驳回", Value = "5" });
                        }
                        if (bm == "")
                        {
                            ddlStatus.Items.Insert(0, new FineUI.ListItem() { Text = "==请选择==", Value = "" });
                            ddlStatus.Items.Insert(1, new FineUI.ListItem() { Text = "通过", Value = "3" });
                            ddlStatus.Items.Insert(2, new FineUI.ListItem() { Text = "驳回", Value = "5" });
                        }
                        if (bm == "1")
                        {
                            ddlStatus.Items.Insert(0, new FineUI.ListItem() { Text = "==请选择==", Value = "" });
                            ddlStatus.Items.Insert(1, new FineUI.ListItem() { Text = "通过", Value = "4" });
                            ddlStatus.Items.Insert(2, new FineUI.ListItem() { Text = "驳回", Value = "8" });
                        }
                    }
                    else
                    {
                        ddlStatus.Hidden = true;
                        ddlStatus.Required = false;
                        txtAduitContent.Hidden = true;
                        txtAduitContent.Required = false;
                    }
                }
                if (ObjectType=="branch") {
                    if (this.CurrentUser.LoginID == "admin")
                    {
                        ddlStatus.Hidden = false;
                    }
                    else {
                        ddlStatus.Hidden = true;
                    }
                }
            }
        }

        #endregion

        #region Events

        private void SaveData()
        {
            string id = GetQueryIntValue("id");
            string imageUrl = string.Empty;
            PartyObjectScoreApply editModel;
            string idddddd = StringHelper.NewID;
            if (id != "")
            {
                //ppMember.Enabled = false;
                //更新
                editModel = DB.PartyObjectScoreApplys.Find(id);
                if (editModel == null)
                {
                    Alert.Show("参数错误！", String.Empty, ActiveWindow.GetHideReference());
                    return;
                }
                editModel.ModifyTime = DateTime.Now;
                editModel.ModifyBy = CurrentUser.LoginID;
                editModel.Status = Convert.ToInt32(ddlStatus.SelectedValue);
            }
            else
            {
                var current = DB.PartyObjectScoreApplys.Find(id);
                editModel = new PartyObjectScoreApply();
                editModel.ID = idddddd;
                if (ObjectType == "branch")
                {
                    if (this.CurrentUser.LoginID == "admin")
                    {
                        editModel.ObjectID = ppBranch.Value;
                        editModel.Status = Convert.ToInt32(ddlStatus.SelectedValue);
                        editModel.ModifyTime = null;
                        editModel.ModifyBy = null;
                    }
                    else {
                        editModel.ObjectID = this.CurrentUser.BranchID;
                        editModel.Status = 2;
                        editModel.ModifyTime = DateTime.Now;
                        editModel.ModifyBy = CurrentUser.LoginID;
                    }
                    editModel.ObjectType = "Branch";
                    editModel.Category = "BranchApplyToBranch";
                }
                else
                {
                    /*if (!string.IsNullOrEmpty(ppMember.Value))
                    {
                        foreach (string item in ppMember.Value.Split(','))
                        {
                            editModel.ObjectID = item;
                        }
                    }
                    else {
                        editModel.ObjectID = this.CurrentUser.LoginID;
                    }*/
                    //如果是党务工作者，可以审批自己
                    string str = this.CurrentUser.IsPartyWorker.ToString();
                    if (str == "True")
                    {
                        editModel.Status = Convert.ToInt32(ddlStatus.SelectedValue);
                    }
                    else {

                        var current1 = DB.PartyOrgBranchs.Find(this.CurrentUser.BranchID);
                        string bm = current1.BranchMark;
                        if (bm == "1") {
                            editModel.Status = 7;
                        }
                        if (bm == "2")
                        {
                            editModel.Status = 3;
                        }
                        if (bm == "3")
                        {
                            editModel.Status = 2;
                        }
                        if (bm == "")
                        {
                            editModel.Status = 2;
                        }

                    }
                    editModel.ObjectID = this.CurrentUser.LoginID;
                    editModel.ObjectType = "Member";
                    editModel.Category = "MemberApplyToBranch";

                }
                editModel.AddType = "";
                editModel.ApplyDate = DateTime.Now;
                
                if (!string.IsNullOrEmpty(hdImageId.Text))
                {
                    editModel.ImageID = hdImageId.Text;
                }

                

                editModel.CreateTime = DateTime.Now;
                
                editModel.CreateBy = CurrentUser.LoginID;
                
                DB.PartyObjectScoreApplys.Add(editModel);
            }
            editModel.Title = txtTitle.Text;
            editModel.Content = txtContent.Text;
            editModel.Score = Convert.ToDecimal(txtScore.Text);
            editModel.AduitContent = txtAduitContent.Text;
            if (DB.SaveChanges() > 0)
            {
                PageContext.RegisterStartupScript(ActiveWindow.GetHidePostBackReference());
            }

            if (ddlStatus.SelectedValue == "4")
            {
                SaveData1(idddddd,id);
            }
        }

        private void SaveData1(string sssss,string id) {
            PartyObjectScore action=new PartyObjectScore();
            var current = DB.PartyObjectScoreApplys.Find(id);
            var s = new PartyObjectScore();
            DB.PartyObjectScores.Add(s);
            
            s.ScoreDate = DateTime.Now;
            if (current == null)
            {
                if (ObjectType == "branch")
                {
                    if (this.CurrentUser.LoginID == "admin")
                    {
                        s.ObjectID = ppBranch.Value;
                    }
                    else
                    {
                        s.ObjectID = this.CurrentUser.BranchID;
                    }
                    s.ObjectType = "Branch";
                    s.Category = "BranchApplyToBranch";
                }
                else
                {
                    s.ObjectID = this.CurrentUser.LoginID;
                    s.ObjectType = "Member";
                    s.Category = "MemberApplyToBranch";

                }
                s.ID = sssss;
                s.Description = txtContent.Text;
                s.Score = Convert.ToDecimal(txtScore.Text);
            }
            else {
                s.ID = id;
                s.ObjectType = current.ObjectType;
                s.ObjectID = current.ObjectID;
                s.Category = current.Category;
                s.Description = current.Content;
                s.Score = Convert.ToDecimal(current.Score);
            }
            s.ScoreType = "";
            s.CreateBy = CurrentUser.LoginID;
            s.CreateTime = DateTime.Now;
            s.ForeignID = "";
            s.Flag = "";
            s.SourceType = "Apply";
            DB.SaveChanges();
        }

        /// <summary>
        /// 保存并关闭按钮
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void btnSaveClose_Click(object sender, EventArgs e)
        {
            SaveData();


        }

        protected void filePhoto_FileSelected(object sender, EventArgs e)
        {
            var rs = UpFileBusiness.Upload(filePhoto, CurrentUser.LoginID);
            if (rs.IsSuccess)
            {
                hdImageId.Text = rs.FileID;
                HyperLink1.ImageID = rs.FileID;
            }
        }


        #endregion

    }
}